package com.duo.controller.admin;

import com.duo.dto.DishDTO;
import com.duo.dto.DishDeleteDTO;
import com.duo.dto.DishPageQueryDTO;
import com.duo.dto.DishUpdateDTO;
import com.duo.result.BaseResult;
import com.duo.result.PageResult;
import com.duo.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Xrx
 * @Description: 菜品管理
 * @CreateTime: 2023/7/6 18:39
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;


    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody @Valid DishDTO dishDTO) {
        dishService.saveWithFlavor(dishDTO);
        return BaseResult.success("新增菜品成功");
    }

    /**
     * 菜品删除
     * @return
     */
    @DeleteMapping("/delete")
    public Object delete(@RequestBody @Valid DishDeleteDTO dishDto) {
        log.info("菜品批量删除：{}", dishDto);
        dishService.delete(dishDto);
        return BaseResult.success("菜品删除成功");
    }


    /**
     * 菜品批量删除
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Object delete(@RequestParam @Valid List<DishDeleteDTO> dishList) {
        log.info("菜品批量删除：{}", dishList);
        dishService.deleteBatch(dishList);
        return BaseResult.success("菜品批量删除成功");
    }

    /**
     * 菜品修改
     * @param dishDTO
     * @return
     */
    @PutMapping("/update")
    public Object update(@RequestBody @Valid DishUpdateDTO dishDTO) {
        log.info("菜品修改：{}", dishDTO);
        dishService.update(dishDTO);
        return BaseResult.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @PostMapping("/page")
    public Object select(@RequestBody @Valid DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return BaseResult.success("菜品分页查询成功", pageResult);
    }


}
